package me.qingy.zookeeper.api_zk.example;

import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;

import java.util.concurrent.CountDownLatch;

/**
 * @author qingy
 * @date 2020-10-02
 */
public class ConfigCenter implements Watcher {

    String IP = "localhost:2181";
    CountDownLatch countDownLatch = new CountDownLatch(1);
    static ZooKeeper zooKeeper;
    static Config config;

    @Override
    public void process(WatchedEvent event) {
        try {
            if (event.getType() == Watcher.Event.EventType.None) {
                if (event.getState() == Watcher.Event.KeeperState.SyncConnected) {
                    System.out.println("连接成功");
                    countDownLatch.countDown();
                }
                // 当配置信息发生变化时
            } else if (event.getType() == Watcher.Event.EventType.NodeDataChanged) {
                System.out.println("监听到配置文件发生改变");
                initValue();
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    // 构造方法
    public ConfigCenter() {
        initValue();
    }


    // 连接zookeeper服务器，读取配置信息
    public void initValue() {
        try {
            // 创建连接对象，需要重新绑定监听
            zooKeeper = new ZooKeeper(IP, 60_000, this);
            // 阻塞线程，等待连接的创建成功
            countDownLatch.await();
            // 读取配置信息
            String url = new String(zooKeeper.getData("/config/url", true, null));
            String username = new String(zooKeeper.getData("/config/username", true, null));
            String password = new String(zooKeeper.getData("/config/password", true, null));

            config = new Config();
            config.setUrl(url);
            config.setUsername(username);
            config.setPassword(password);

        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }


    public static void main(String[] args) {
        try {
            ConfigCenter configCenter = new ConfigCenter();
            for (int i = 1; i <= 20; i++) {
                Thread.sleep(5000);
                System.out.println(config);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    // 配置信息
    static class Config {

        private String url;
        private String username;
        private String password;

        public String getUrl() {
            return url;
        }

        public void setUrl(String url) {
            this.url = url;
        }

        public String getUsername() {
            return username;
        }

        public void setUsername(String username) {
            this.username = username;
        }

        public String getPassword() {
            return password;
        }

        public void setPassword(String password) {
            this.password = password;
        }

        @Override
        public String toString() {
            return "Config{" +
                    "url='" + url + '\'' +
                    ", username='" + username + '\'' +
                    ", password='" + password + '\'' +
                    '}';
        }
    }
}
